//
//  MainViewController.swift
//  Easy_Car_Treasure_Swift
//
//  Created by 谭鹏 on 2017/6/5.
//  Copyright © 2017年 谭鹏. All rights reserved.
//

import UIKit

class MainViewController: UIViewController {
    
    var navigationC: UINavigationController?
    var operationTabBarC: TP_OperationTabBarController!
    // MARK: - life
    override func viewDidLoad() {
        super.viewDidLoad()
        
        NotificationCenter.default.addObserver(self, selector: #selector(MainViewController.gotoLoginVC), name: NSNotification.Name(rawValue: NOTIFICATION__GOTO_LOGIN), object: nil)
        
        NotificationCenter.default.addObserver(self, selector: #selector(MainViewController.gotoOpertionVC), name: NSNotification.Name(rawValue: NOTIFICATION__GOTO_OPERATION), object: nil)
        
        self.navigationC = UINavigationController(rootViewController:TP_LoginViewController())
        self.operationTabBarC = TP_OperationTabBarController()
        self.operationTabBarC.tabBar.backgroundColor = TP_Color.white;
        
        let  token: String? = UserDefaults.standard.object(forKey: REQUEST_TOKEN) as? String
        
//        if (token == nil) {
//            
//            self.gotoLoginVC()
//            
//        }else{
        
            self.gotoOpertionVC()
            
//        }
        
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    
    // MARK: - action
    
    func gotoLoginVC() {
        self.gotoVC(newVC: self.navigationC!)
        
    }
    
    func gotoOpertionVC(){
         self.gotoVC(newVC: self.operationTabBarC!)
    }
    
    func gotoVC(newVC:UIViewController) {
        let existVC = self.childViewControllers.first
        self.addChildViewController(newVC)
        self.view.addSubview((newVC.view)!)
        
        if let existedVC = existVC {
            
            
            self.transition(from: existVC!, to: newVC, duration: 0.5, options: UIViewAnimationOptions.transitionFlipFromTop, animations: {
                newVC.view.alpha = 1
                existedVC.view.alpha = 0
            }, completion: { (animated: Bool) in
                existedVC.view.removeFromSuperview()
                existedVC .removeFromParentViewController()
            })
        }
    }
    
    
    
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
